package com.weiyirong99.crm.app.server;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;

import androidx.core.app.NotificationCompat;


import com.google.gson.Gson;
import com.tencent.mmkv.MMKV;
import com.weiyirong99.crm.app.R;
import com.weiyirong99.crm.app.api.Url;
import com.weiyirong99.crm.app.ui.LoginActivity;
import com.weiyirong99.crm.app.ui.MainActivity;

import java.util.HashMap;
import java.util.Map;


/**
 * @Description:
 * @Author: zdx
 * @CreateDate: 2020/9/21
 * @UpdateDate: 2020/9/21
 * @Version: 1.0
 */
public class WorkAloneService extends Service {

    private Callback callback = null;
    public int data = 0;
    private boolean destroyed = false;

    OkHttp3Util http = new OkHttp3Util();
    public boolean thread_running = true;

    public static String strSerialNum = "";
    public static String strServerHost = "";


    private PendingIntent mPi;
    private int number = 0;
    private NotificationCompat.Builder notification; //创建服务对象

    public WorkAloneService() {
    }

    public class MyBinder extends Binder {

        public WorkAloneService getService() {
            return WorkAloneService.this;
        }

        public void setRunningStatus(boolean running) {
            thread_running = running;
        }

        public int getData() {
            return data;
        }

    }

    /**
     * 系统保活动作，提升Service权限保持持久性
     */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        return Service.START_STICKY_COMPATIBILITY;
    }


    public class QueryThread extends Thread {
        @Override
        public void run() {
            while (thread_running) {
                try {

                    http.sendByGetUrl(Url.ping, MMKV.defaultMMKV().decodeString("token"));
                    sleep(10000);
                } catch (Exception e) {
                    callback.onDataChange(0, e.toString());
                }
            }
        }
    }


    @Override
    public void onCreate() {
        super.onCreate();
        new QueryThread().start();
        Log.e("WG", "onStartCommand: 进来了");
        boolean loginFlag = MMKV.defaultMMKV().decodeBool("loginFlag");
        String access_token = MMKV.defaultMMKV().decodeString("token");
        if (!TextUtils.isEmpty(access_token) && !loginFlag) {
            Intent intent = new Intent(this, MainActivity.class);
            mPi = PendingIntent.getActivities(this, 0, new Intent[]{intent}, PendingIntent.FLAG_IMMUTABLE);
        } else {
            Intent intent = new Intent(this, LoginActivity.class);
            mPi = PendingIntent.getActivities(this, 0, new Intent[]{intent}, PendingIntent.FLAG_IMMUTABLE);
        }
        String ID = "com.weiyirong99.crm.app.MainActivity";    //这里的id里面输入自己的项目的包的路径
        String NAME = "Channel One";

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(ID, NAME, manager.IMPORTANCE_HIGH);
            channel.enableLights(true);
            channel.setShowBadge(true);
            channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            manager.createNotificationChannel(channel);
            notification = new NotificationCompat.Builder(this).setChannelId(ID);
        } else {
            notification = new NotificationCompat.Builder(this);
        }

        notification.setContentTitle("CRM")
                .setContentText("运行中...")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.logo)
                .setContentIntent(mPi)
                .build();

        Notification notification1 = notification.build();
        notification1.flags = Notification.FLAG_ONGOING_EVENT;
        manager.notify(1, notification1);

    }


    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    public Callback getCallback() {
        return callback;
    }

    public interface Callback {
        void onDataChange(int what, Object data);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("WG", "onDestroy: 结束了");
        destroyed = true;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }
}
